package algorithms.roughgarden.part2.primmst;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;


public class Main {

	/**
	 * @param argsj
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		InputStream is = Main.class.getResourceAsStream("edges.txt");
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		String str;
		str = br.readLine();
		String[] strArr = str.split(" ");
		int noOfVertices  = Integer.parseInt(strArr[0]);
		int noOfEdges = Integer.parseInt(strArr[1]);
		Graph graph = new Graph((short)noOfVertices,(short)noOfEdges);
		while((str=br.readLine())!=null){
			strArr = str.split(" ");
			graph.addEdgeWithWeight(Short.parseShort(strArr[0]), Short.parseShort(strArr[1]), Integer.parseInt(strArr[2]));
		}
		List<Edge> mstEdges = graph.calculateMst();
		int sum = 0;
		for(Edge e:mstEdges){
			sum+=e.getWeight();
		}
		System.out.println(sum);
	}

}
